package com.gitee.mp.configure;

import com.gitee.mp.EnabledMilvusPlus;
import com.gitee.mp.core.interceptor.*;
import com.gitee.mp.core.mapper.MilvusMapperFactory;
import com.gitee.mp.core.parser.DefaultDefinitionParser;
import com.gitee.mp.core.connector.MilvusConnectorFactory;
import com.gitee.mp.extend.*;
import com.gitee.mp.infra.utils.ApplicationContextUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author hongda.li
 */
@Configuration(proxyBeanMethods = false)
@ComponentScan(basePackageClasses = EnabledMilvusPlus.class)
public class MilvusConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public DefinitionParser definitionParser() {
        return new DefaultDefinitionParser();
    }

    @Bean
    public ApplicationContextUtils contextHelper() {
        return new ApplicationContextUtils();
    }

    @Bean
    @ConditionalOnMissingBean
    public CollectionChangeable dynamicCollectionSwitch() {
        return origin -> origin;
    }

    @Bean
    @ConditionalOnMissingBean
    public MilvusConnectorFactory connectorFactory(List<MilvusConfigureChain> configureChains) {
        return new MilvusConnectorFactory(configureChains);
    }

    @Bean
    @ConditionalOnMissingBean
    public MilvusMapperFactory milvusAccessorFactory(DefinitionParser definitionParser,
                                                     MilvusConnectorFactory connectorFactory,
                                                     List<MapperInterceptor> interceptorList) {
        return new MilvusMapperFactory(definitionParser, connectorFactory, interceptorList);
    }

    @Bean
    @ConditionalOnMissingBean
    public BatchInsertInterceptor insertInterceptor() {
        return new BatchInsertInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public BatchUpsertInterceptor upsertInterceptor() {
        return new BatchUpsertInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public BatchDeleteInterceptor deleteInterceptor() {
        return new BatchDeleteInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public SelectInterceptor findListInterceptor() {
        return new SelectInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public ResultMappingInterceptor resultMappingInterceptor() {
        return new ResultMappingInterceptor();
    }
}
